package br.com.condominio.entity;
// default package
// Generated 19/04/2012 00:12:16 by Hibernate Tools 3.4.0.CR1

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "blo_descricao"))
@SequenceGenerator(name = "bloco_blo_codigo_seq", sequenceName = "bloco_blo_codigo_seq", allocationSize = 1)
public class Bloco implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Integer bloCodigo;
	private Residencial residencial;
	private String bloDescricao;
	private Set<Responsavel> responsavels = new HashSet<Responsavel>(0);
	private Set<Apartamento> apartamentos = new HashSet<Apartamento>(0);
	
	private String strResBlo;

	public Bloco() {
	}

	public Bloco(Integer bloCodigo, Residencial residencial,
			String bloDescricao) {
		this.bloCodigo = bloCodigo;
		this.residencial = residencial;
		this.bloDescricao = bloDescricao;
	}

	public Bloco(Integer bloCodigo, Residencial residencial,
			String bloDescricao, Set<Responsavel> responsavels,
			Set<Apartamento> apartamentos) {
		this.bloCodigo = bloCodigo;
		this.residencial = residencial;
		this.bloDescricao = bloDescricao;
		this.responsavels = responsavels;
		this.apartamentos = apartamentos;
	}

	@Id
	@Column(name = "blo_codigo", unique = true, nullable = false, precision = 131089, scale = 0)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "bloco_blo_codigo_seq")
	public Integer getBloCodigo() {
		return this.bloCodigo;
	}

	public void setBloCodigo(Integer bloCodigo) {
		this.bloCodigo = bloCodigo;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "res_codigo", nullable = false)
	public Residencial getResidencial() {
		if(residencial == null)
			residencial = new Residencial();
		return this.residencial;
	}

	public void setResidencial(Residencial residencial) {
		this.residencial = residencial;
	}

	@Column(name = "blo_descricao", unique = true, nullable = false, precision = 131089, scale = 0)
	public String getBloDescricao() {
		return this.bloDescricao;
	}

	public void setBloDescricao(String bloDescricao) {
		this.bloDescricao = bloDescricao;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "bloco")
	public Set<Responsavel> getResponsavels() {
		if(this.responsavels == null)
			responsavels = new HashSet<Responsavel>(0);
		return this.responsavels;
	}

	public void setResponsavels(Set<Responsavel> responsavels) {
		this.responsavels = responsavels;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "bloco")
	public Set<Apartamento> getApartamentos() {
		if(this.apartamentos == null)
			apartamentos = new HashSet<Apartamento>(0);
		return this.apartamentos;
	}

	public void setApartamentos(Set<Apartamento> apartamentos) {
		this.apartamentos = apartamentos;
	}
	
	@Transient
	public String getStrResBlo() {
		
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append(getResidencial().getResNome() +" - ");
		stringBuffer.append(getBloDescricao());
		
		this.strResBlo = stringBuffer.toString();
		
		return strResBlo;
	}

	public void setStrResBlo(String strResBlo) {
		this.strResBlo = strResBlo;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((bloCodigo == null) ? 0 : bloCodigo.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Bloco other = (Bloco) obj;
		if (bloCodigo == null) {
			if (other.bloCodigo != null)
				return false;
		} else if (!bloCodigo.equals(other.bloCodigo))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Bloco [bloCodigo=" + bloCodigo + ", residencial=" + residencial
				+ ", bloDescricao=" + bloDescricao + "]";
	}
	
	

}
